package com.lyc.assistant.base

import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.lyc.assistant.R
import com.lyc.assistant.databinding.ActivityMainBinding
import com.lyc.assistant.translate.activity.OfflineTranslateActivity
import com.translation.assistant.callback.TranslateCallback
import com.translation.assistant.manager.OfflineTranslatorManager

class MainActivity: BaseActivity<ActivityMainBinding>() {
    private lateinit var translator: OfflineTranslatorManager
    private lateinit var adapter: ArrayAdapter<String>
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initViewsAndEvents(savedInstanceState: Bundle?) {
        translator = OfflineTranslatorManager(this@MainActivity)
        mDataBinding.apply {
            val data = arrayOf(
                "离线翻译",
                "text",
            )
            adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_list_item_1, data)
            listView.adapter = adapter
            listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
                onItemClick(position)
            }
        }
    }

    private fun onItemClick(position: Int) {
        when(adapter.getItem(position)){
            "离线翻译" -> {
                startActivity(OfflineTranslateActivity::class.java)
            }
            "text" -> {
                translator.translate("Hello,How are you!","en","zh",object :
                    TranslateCallback {
                    override fun onCompleted(
                        data: String?,
                        fromLangCode: String?,
                        toLangCode: String?,
                        status: Boolean,
                    ) {
                        mXHandler.post { mDataBinding.tvResult.text = data }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        mXHandler.post { mDataBinding.tvResult.text = msg }
                    }
                })
            }
        }
    }
}